﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
 * ### HTTP://THEAJ.NET/ <AJ@THEAJ.NET> ### *
\* ######################################## */
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

using AJRavindiran.Jolt.Utilities.Parsing;

namespace AJRavindiran.Jolt
{
    /// <summary>
    /// The startup class.
    /// </summary>
    public class Program
    {
        #region Fields
        /// <summary>
        /// Determines if the console should display all the
        /// errors with detailed error messages & stacks.
        /// </summary>
        public static bool DebugMode = false;

        /// <summary>
        /// Determines if the console should beep when
        /// an error is shown.
        /// </summary>
        public static bool BeepEnabled = false;
        #endregion

        #region Methods
        public static void Main(string[] args)
        {
            // Load all drivers needed for JoltEnvironment.
            AppDomain.CurrentDomain.AssemblyResolve += FindDrivers;

            // Load settings for the console.
            int consoleHeight = Convert.ToInt32(INI.ReadValue(@"./Bin/CONSOLE", "CONSOLE SETTINGS", "console.height"));
            int consoleWidth = Convert.ToInt32(INI.ReadValue(@"./Bin/CONSOLE", "CONSOLE SETTINGS", "console.width"));
            bool resizeEnabled = Convert.ToBoolean(INI.ReadValue(@"./Bin/CONSOLE", "CONSOLE SETTINGS", "console.resize_enabled"));
            DebugMode = Convert.ToBoolean(INI.ReadValue(@"./Bin/CONSOLE", "CONSOLE SETTINGS", "console.debug_mode"));
            BeepEnabled = Convert.ToBoolean(INI.ReadValue(@"./Bin/CONSOLE", "CONSOLE SETTINGS", "console.beep_enabled"));

            Console.Title = "Jolt Environment: RuneScape Emulator";

            // If resizeing is enabled by the user, we will resize the console.
            if (resizeEnabled)
            {
                Console.WindowHeight = Console.LargestWindowHeight - consoleHeight;
                Console.WindowWidth = Console.LargestWindowWidth - consoleWidth;
            }

            // The banner displayed @ startup.
            // DO NOT REMOVE/EDIT THIS BANNER.
            Console.WriteLine("######################################################################");
            Console.WriteLine("###              Jolt Environment: RuneScape Emulator              ###");
            Console.WriteLine("### -------------------------------------------------------------- ###");
            Console.WriteLine("###                Copyright (C) 2009 AJ Ravindiran                ###");
            Console.WriteLine("###                HTTP://THEAJ.NET/ <AJ@THEAJ.NET>                ###");
            Console.WriteLine("######################################################################");
            Console.WriteLine();
            Console.WriteLine();

            // Initialize the Jolt Environment.
            JoltEnvironment.Initialize();

            // A loop that waits for the user to press a key.
            // if the user presses a key, it will promt the user
            // to press any key again. If the user does so, it
            // will shutdown Jolt Environment & RuneScape Emulator.
            while (true)
            {
                Console.ReadKey(true);
                JoltEnvironment.GetLogger().WriteInfo("To confirm shutdown, press any key again...");
                Console.ReadKey(true);
                JoltEnvironment.ShutDown();
            }
        }

        /// <summary>
        /// Finds and loads all Drivers needed.
        /// </summary>
        private static Assembly FindDrivers(object sender, ResolveEventArgs args)
        {
            string drivers = new AssemblyName(args.Name).Name;
            string path = @".\Bin\Drivers\" + drivers + ".dll";

            if (!File.Exists(path)) return null;
            return Assembly.LoadFrom(path);
        }
        #endregion
    }
}
